package com.edgclub.edg.fragment;

import android.app.Activity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.android.volley.VolleyError;
import com.edgclub.edg.R;
import com.edgclub.edg.adapter.FindVideoAdapter;
import com.edgclub.edg.api.FindApi;
import com.edgclub.edg.api.ResultCode;
import com.edgclub.edg.api.param.PostsParam;
import com.edgclub.edg.event.BroadCastEvent;
import com.edgclub.edg.event.EventBus;
import com.edgclub.edg.http.VolleyListener;
import com.edgclub.edg.model.User;
import com.edgclub.edg.model.VideoBean;
import com.edgclub.edg.model.result.VideoData;
import com.edgclub.edg.utils.GlideUtil.GlideUtils;
import com.edgclub.edg.utils.util.JSONHelper;
import com.edgclub.edg.utils.util.JsonUtil;
import com.edgclub.edg.utils.util.UserPrefUtils;
import com.jcodecraeer.xrecyclerview.XRecyclerView;
import com.squareup.otto.ThreadEnforcer;

import java.util.ArrayList;

/**
 * 视频
 */
public class FindVideoFragment extends GzBaseFragment {

    private Activity mActivity;

    private XRecyclerView recyclerView;
    private FindVideoAdapter videoAdapter;
    private ArrayList<VideoBean> datas;

    private UserPrefUtils userPrefUtils;
    private FindApi findApi;
    private GlideUtils glideUtils;
    private boolean isFirstLoad = true;


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        mActivity = this.getActivity();
        return inflater.inflate(R.layout.fragment_find_video, container, false);
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EventBus.getEventBus(BroadCastEvent.EVENTBUS_COMMON,
                ThreadEnforcer.MAIN).register(this);
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initView(view);
        initData();
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        if(isVisibleToUser && isFirstLoad){
            isFirstLoad = false;
            getVideoList();
        }else{

        }
    }


    @Override
    public void onPause() {
        super.onPause();
    }

    private void initView(View view) {
        initLoadView(view);
        recyclerView = (XRecyclerView) view.findViewById(R.id.recyclerview);
        LinearLayoutManager layoutManager = new LinearLayoutManager(mActivity);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(layoutManager);
        recyclerView.setLoadingMoreEnabled(false);
        recyclerView.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                getVideoList();
            }

            @Override
            public void onLoadMore() {

            }
        });

    }

    private void initData() {
        findApi = new FindApi(getActivity());
        userPrefUtils = new UserPrefUtils(getActivity());
        datas = new ArrayList<>();
        glideUtils = new GlideUtils();

        videoAdapter = new FindVideoAdapter(getActivity());
        videoAdapter.setDatas(datas);
        recyclerView.setAdapter(videoAdapter);

    }

    private boolean edgLoading = false;
    private boolean isEdgEnd = false;

    private void getVideoList() {
        if (edgLoading || isEdgEnd) {
            return;
        }
        User user = userPrefUtils.getUser();
        PostsParam nearbyParam = new PostsParam();
        nearbyParam.setUserId(user.getId() + "");
        findApi.video(nearbyParam, new getIndexListener());//关注列表
    }


    class getIndexListener implements VolleyListener {

        @Override
        public void onStart(String tag) {
            edgLoading = true;
        }

        @Override
        public void onSuccess(String response, String tag) {

            if (JSONHelper.isSuccess(response)) {
                VideoData data = JsonUtil.getMode(response, VideoData.class);
                if (null != data && null != data.getData()) {
                    datas.clear();
                    datas.addAll(data.getData());
                    videoAdapter.notifyDataSetChanged();
                }
            } else {

            }

            edgLoading = false;
            recyclerView.refreshComplete();
            recyclerView.loadMoreComplete();
            setLoadViewGONE();
        }

        @Override
        public void onFaile(VolleyError volleyError, String tag) {
            edgLoading = false;
            recyclerView.refreshComplete();
            recyclerView.loadMoreComplete();
            setNoNetWork(new ReLoadClickListener() {
                @Override
                public void reload() {
                    getVideoList();
                }
            });
            ResultCode.toastVolleyError(getActivity(), volleyError);
        }
    }
    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getEventBus(BroadCastEvent.EVENTBUS_COMMON,
                ThreadEnforcer.MAIN).unregister(this);
    }
}
